package com.wwwarehouse.contract.program_operation.resource_factor_ship;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;

import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.contract.bean.CustomerAndResListBean;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.ResourceFactorShipCheckEvent;
import com.wwwarehouse.contract.event.SelectResourceFactorShipEvent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/23.
 * 选择资源要素viewpager
 */

public class SelectResourceFactorViewPagerFragment extends BaseViewPagerFragment implements View.OnClickListener {
	private List<CustomerAndResListBean.Customers> list = new ArrayList<>();
	private List<CustomerAndResListBean.Customers> mLastCheckedList = new ArrayList<>();
	private List<CustomerAndResListBean.Customers> mCheckList = new ArrayList<>();
	private List<CustomerAndResListBean.Customers> mGangCaiDianDe = new ArrayList<>();

	private int position;
	int num = 0;
	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mCheckList.clear();
		mConfirmBtn.setVisibility(View.VISIBLE);
		EventBus.getDefault().register(this);
		mCustomScroll.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
		mConfirmBtn.setOnClickListener(this);
	}

	@Override
	public void onResponse(String response, int what) {

	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof SelectResourceFactorViewPagerFragment) {
			mActivity.setTitle(R.string.choose_resources_title);
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getParcelableArrayList(ContractConstant.KEY_BUNDLE_PARA_DATA) != null) {
			list.clear();
			position = getArguments().getInt(ContractConstant.KEY_BUNDLE_CUSTOMER_POSITION);
			list = (List<CustomerAndResListBean.Customers>) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_PARA_DATA);
			boolean canClick = false;

			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).isSelected()) {
					canClick = true;
					num++;
				}
			}
			if (canClick) {
				mConfirmBtn.setEnabled(true);
				mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
			} else {
				mConfirmBtn.setEnabled(false);
				mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
			}
			mActivity.setTitle("需要采购的资源(" +num + ")");

			setData((Serializable) list, list.size(), 6, new SelectResourceFactorFragment().getClass().getName() , false);

//			if (list.size() > 0) {
//				if (mLastCheckedList.size() > 0) {
//					for (int i = 0; i < mLastCheckedList.size(); i++) {
//						for (int j = 0; j < list.size(); j++) {
//							if (mLastCheckedList.get(i).getCode().equals(list.get(j).getCode())) {
//								list.get(j).setSelected(mLastCheckedList.get(i).isSelected());
//							}
//						}
//					}
//
//
//
//				}
//			}

		}
	}

	public void onEventMainThread(SelectResourceFactorShipEvent event) {
//step1  只添加手动选中的条目
		if (!mCheckList.contains(event.getmChecked()) && event.getmChecked().isSelected()) {
			mCheckList.add(event.getmChecked());
		} else if (mCheckList.contains(event.getmChecked()) && !event.getmChecked().isSelected()){
			mCheckList.remove(event.getmChecked());
		}

		//step 2 从两个List中删选， 如果把默认选中的条目手动取消了，则去默认选中的容器中删除，保证两个容器条目不重复。
		for (int i = 0; i < mLastCheckedList.size(); i++) {
			if (!event.getmChecked().isSelected() && event.getmChecked().getCode().equals(mLastCheckedList.get(i).getCode())) {
				mGangCaiDianDe.add(mLastCheckedList.get(i));
				mLastCheckedList.remove(i);
			}

		}

		if (event.getmChecked().isSelected()) {
			num++;
		} else {
			num--;
		}



		//step 3 合并两个容器返回上层
		List<CustomerAndResListBean.Customers> finalList = new ArrayList<>();
		finalList.addAll(mCheckList);
		finalList.addAll(mLastCheckedList);

		//
		if (num > 0) {
			mActivity.setTitle("需要采购的资源(" +num + ")");
			mConfirmBtn.setEnabled(true);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
		} else {
			mActivity.setTitle("需要采购的资源");
			mConfirmBtn.setEnabled(false);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.confirm_btn) {
			List<CustomerAndResListBean.Customers> finalList = filterData();
			for (int i = 0; i < list.size(); i++) {
				for (int j = 0; j < finalList.size(); j++) {
					if (finalList.get(j).getName().equals(list.get(i))) {
						list.get(i).setSelected(finalList.get(j).isSelected());
					}
				}
			}
			EventBus.getDefault().post(new ResourceFactorShipCheckEvent( position,finalList));
			popFragment();
		}
	}

	@NonNull
	private List<CustomerAndResListBean.Customers> filterData() {
		List<CustomerAndResListBean.Customers> finalList = new ArrayList<>();
		finalList.addAll(mCheckList);
		finalList.addAll(mLastCheckedList);
		return finalList;
	}

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (backListenerEvent.getMsg().equals("SelectResourceFactorViewPagerFragment")) {
			mLastCheckedList.addAll(mGangCaiDianDe);
			EventBus.getDefault().post(new ResourceFactorShipCheckEvent(position ,mLastCheckedList));
			popFragment();
		}
	}

}